<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Relations\UserInfoTrait;
use App\Models\Relations\CommonTrait;
use Illuminate\Support\Facades\DB;

/**
 * 课程具体内容
 * 与 章节 多对一
 * @author Joker
 * chapter_id int(11) NOT NULL COMMENT '章节编号',
	`name` varchar(200) NOT NULL COMMENT '标题',
	`description` text NOT NULL COMMENT '简介',
	`tools` text NOT NULL COMMENT '工具',
	`content` longtext NOT NULL COMMENT '步骤  序列化字符串',
	`notice` text DEFAULT NULL COMMENT '注意事项',
	`user_id` int(11) NOT NULL COMMENT '创建用户编号',
	`sort` int(10) DEFAULT NULL COMMENT '越大越靠前',
	`teaches_type` tinyint(1) NOT NULL DEFAULT 0 COMMENT '充电加油文章类型：0 经验文章；1 ：视频文章 2:ppt文章 默认 0',
	price MEDIUMINT(8) NOT NULL DEFAULT 0 COMMENT '价格 默认为0 免费',
	`views_num` int(10) DEFAULT 0 COMMENT '浏览量',
	`studys_num` int(10) DEFAULT 0 COMMENT '当前学习人数',
	`attention_num` int(10) DEFAULT 0 COMMENT '收藏数量',
	`praises_num` int(10) DEFAULT 0 COMMENT '点赞数量',
	`comments_num` int(10) DEFAULT 0 COMMENT '评论数量',
 */
class TeachesContent extends Model
{
    use SoftDeletes,UserInfoTrait,CommonTrait;
    
    protected $table="teaches_content";
    
    protected $fillable = ['id','chapter_id','name','description','tools','content','notice','user_id','sort','teaches_type',
                        'views_num','studys_num','attention_num','praises_num','comments_num'
    ];
    
    protected $dates = ['deleted_at'];
    
    /**
     * 获取章节信息
     */
    public function chapter()
    {
        return $this->belongsTo('App\Models\TeachesChapter');
    }
    
    /**
     * 一对一获取 视频信息
     */
    public function office()
    {
        return $this->hasOne('App\Models\TeachesContentOffice','content_id','id');
    }
    
    /**
     * 一对一获取 信息
     */
    public function video()
    {
        return $this->hasOne('App\Models\TeachesContentVideo','content_id','id');
    }
    
    public function ppt()
    {
        return $this->hasOne('App\Models\TeachesContentPpt','content_id','id');
    }
    
    public static  function teachesCount($teachesid)
    {
        $sql = "SELECT * FROM qm_teaches_content where chapter_id in (SELECT id FROM qm_teaches_chapter where teaches_id = ? and deleted_at is null) and deleted_at is null;";
        return DB::select($sql,[$teachesid]);
    }

}